import datetime
import requests
from views import redis_tool

UserName = '18665923312'
Password = 'liuxiang719'
# 获取屠龙刀token
def get_tld_token():
    try:
        params = {"username": UserName, "password": Password}
        re = requests.get("http://8.217.203.153:8090/api/v1/login", params=params, timeout=20)
        if re.status_code == 200:
            redis_tool.set_tld_token(re.json()['data']['token'])
            print('token更新成功')
            login_count = redis_tool.get_login_count()
            new_count = int(login_count) + 1
            redis_tool.set_login_count(new_count)
        return {'code': 1, 'messge': '更新成功','data':{'count':new_count}}
    except Exception as e:
        return ''

#获取屠龙刀精选
def get_tld_selected_stocks(token, dateStr = datetime.datetime.now().strftime('%Y-%m-%d')):
    params = {'date': dateStr}
    # params = {'date': '2025-01-15'}
    re = requests.get("http://8.217.203.153:8090/api/v1/stock/focus_by_date", params=params, headers={'Token': token})
    return re.json()

#获取屠龙刀候选池
def get_tld_montior_stocks(token, dateStr = datetime.datetime.now().strftime('%Y-%m-%d')):
    params = {'date': dateStr}
    # params = {'date': '2025-01-13'}
    re = requests.get("http://8.217.203.153:8090/api/v1/stock/monitor", params=params, headers={'Token': token})
    return re.json()

def get_last(token):
    re = requests.get("http://8.217.203.153:8090/api/v1/index/last", headers={'Token': token})
    return re.json()

def get_tld_top5_concept(token):
    lasts = redis_tool.get_last_date()
    if len(lasts) > 0:
        params = {'date': lasts[0], "top": 5}
        re = requests.get("http://8.217.203.153:8090/api/v1/topics/ranking?", params = params, headers={'Token': token})
        return re.json()

# 获取屠龙刀监控提示股票
def get_tld_montior_info(token):
    dateStr = datetime.datetime.now().strftime('%Y-%m-%d')
    params = {'date': dateStr, 'last_time': 0}
    re = requests.get("http://8.217.203.153:8090/api/v1/stock/monitor_message", params=params, headers={'Token': token})
    return re.json()